﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.Collision.CApRJCollisionQueryMgr
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RobotJog.Collision;

internal class CApRJCollisionQueryMgr
{
  private TxCollisionQueryContext m_queryGlobalContext;
  private TxCollisionQueryContext m_queryLocalContext;
  private eFilterType m_selectedFilter;
  private bool m_checkCollisions;
  private TxCollisionQueryParams m_dynamicCollisionQueryParams = new TxCollisionQueryParams();

  internal CApRJCollisionQueryMgr() => this.Init();

  public TxCollisionQueryResults GetQueryResults()
  {
    TxCollisionQueryResults queryResults = (TxCollisionQueryResults) null;
    switch (this.SelectedFilter)
    {
      case eFilterType.CollisionsOnly:
        queryResults = this.GetCollisionAndNearMissResults(false);
        break;
      case eFilterType.DistanceAnalysis:
        queryResults = this.GetCollisionAndDistancesResults(false);
        break;
    }
    return queryResults;
  }

  public TxCollisionQueryResults GetAllCollisionsQueryResults()
  {
    TxCollisionQueryResults collisionsQueryResults = (TxCollisionQueryResults) null;
    switch (this.SelectedFilter)
    {
      case eFilterType.CollisionsOnly:
        collisionsQueryResults = this.GetCollisionAndNearMissResults(true);
        break;
      case eFilterType.DistanceAnalysis:
        collisionsQueryResults = this.GetCollisionAndDistancesResults(true);
        break;
    }
    return collisionsQueryResults;
  }

  public void ResetCheckCollisions()
  {
    this.m_checkCollisions = TxApplication.ActiveDocument.CollisionRoot.CheckCollisions;
    if (!this.m_checkCollisions)
      return;
    this.SetQueryContext();
  }

  public void ResetQueryContext()
  {
    if (this.IsLocalContextUsed)
      this.ResetQueryLocalContext();
    else
      this.ResetQueryGlobalContext();
  }

  public void Uninitialize()
  {
    try
    {
      this.UninitDocumentContext();
      TxApplication.DocumentCollection.DocumentLoaded -= new TxDocumentCollection_DocumentLoadedEventHandler(this.OnLoadDocument);
    }
    catch (Exception ex)
    {
    }
  }

  public void InitDynamicCollisionQueryParams(TxCollisionQueryParams queryParams)
  {
    TxCollisionRoot collisionRoot = TxApplication.ActiveDocument.CollisionRoot;
    queryParams.Mode = collisionRoot.CollisionQueryMode;
    queryParams.ComputeDistanceIfInNearMiss = true;
    queryParams.NearMissDistance = collisionRoot.NearMissDefaultValue;
    queryParams.UseNearMiss = collisionRoot.CheckNearMiss;
    queryParams.AllowedPenetration = (double) TxApplication.Options.Collision.ContactTolerance;
    queryParams.UseAllowedPenetration = TxApplication.Options.Collision.CheckContact;
    queryParams.FindPenetrationRegions = TxApplication.Options.Collision.FindPenetrationRegions;
    queryParams.ReportLevel = TxApplication.Options.Collision.ReportLevel;
  }

  private TxCollisionQueryResults GetGlobalCollidingObjectsChanges(
    TxCollisionQueryParams queryParams)
  {
    return TxApplication.ActiveDocument.CollisionRoot.GetGlobalCollidingObjectsChanges(queryParams, this.QueryContext);
  }

  private TxCollisionQueryResults GetCollisionAndNearMissResults(bool all)
  {
    TxCollisionQueryResults andNearMissResults = (TxCollisionQueryResults) null;
    if (this.QueryContext != null)
    {
      try
      {
        TxCollisionRoot collisionRoot = TxApplication.ActiveDocument.CollisionRoot;
        TxCollisionQueryParams queryParams = new TxCollisionQueryParams();
        queryParams.Mode = collisionRoot.CollisionQueryMode;
        queryParams.ComputeDistanceIfInNearMiss = true;
        queryParams.NearMissDistance = collisionRoot.NearMissDefaultValue;
        queryParams.UseNearMiss = collisionRoot.CheckNearMiss;
        queryParams.AllowedPenetration = (double) TxApplication.Options.Collision.ContactTolerance;
        queryParams.UseAllowedPenetration = TxApplication.Options.Collision.CheckContact;
        queryParams.FindPenetrationRegions = TxApplication.Options.Collision.FindPenetrationRegions;
        queryParams.ReportLevel = TxApplication.Options.Collision.ReportLevel;
        if (all)
          this.ResetQueryContext();
        return this.GetGlobalCollidingObjectsChanges(queryParams);
      }
      catch (Exception ex)
      {
      }
    }
    return andNearMissResults;
  }

  private TxCollisionQueryResults GetCollidingObjectsAndDistancesChanges(
    TxCollisionAndDistancesQueryParams queryParams)
  {
    return TxApplication.ActiveDocument.CollisionRoot.GetCollidingObjectsAndDistancesChanges(queryParams, this.QueryContext);
  }

  private TxCollisionQueryResults GetCollisionAndDistancesResults(bool all)
  {
    try
    {
      TxCollisionRoot collisionRoot = TxApplication.ActiveDocument.CollisionRoot;
      TxCollisionAndDistancesQueryParams queryParams = new TxCollisionAndDistancesQueryParams();
      queryParams.Mode = collisionRoot.CollisionQueryMode;
      queryParams.NearMissDistance = collisionRoot.NearMissDefaultValue;
      queryParams.AllowedPenetration = (double) TxApplication.Options.Collision.ContactTolerance;
      queryParams.UseAllowedPenetration = TxApplication.Options.Collision.CheckContact;
      queryParams.UseNearMiss = collisionRoot.CheckNearMiss;
      queryParams.FindPenetrationRegions = TxApplication.Options.Collision.FindPenetrationRegions;
      queryParams.ReportLevel = TxApplication.Options.Collision.ReportLevel;
      if (all)
        this.ResetQueryContext();
      return this.GetCollidingObjectsAndDistancesChanges(queryParams);
    }
    catch (Exception ex)
    {
      return (TxCollisionQueryResults) null;
    }
  }

  private void ResetQueryLocalContext()
  {
    try
    {
      if (this.m_queryLocalContext == null)
        return;
      TxApplication.ActiveDocument.CollisionRoot.ResetQueryContext(this.m_queryLocalContext);
    }
    catch (Exception ex)
    {
    }
  }

  private void ResetQueryGlobalContext()
  {
    try
    {
      if (this.m_queryGlobalContext == null)
        return;
      TxApplication.ActiveDocument.CollisionRoot.ResetGlobalQueryContext(this.m_queryGlobalContext);
    }
    catch (Exception ex)
    {
    }
  }

  private void SetQueryContext()
  {
    TxCollisionRoot collisionRoot = TxApplication.ActiveDocument.CollisionRoot;
    if (this.m_queryGlobalContext == null)
      this.m_queryGlobalContext = collisionRoot.GlobalQueryContext;
    if (this.m_queryLocalContext != null)
      return;
    this.m_queryLocalContext = collisionRoot.CreateQueryContext();
  }

  private void Init()
  {
    try
    {
      TxApplication.DocumentCollection.DocumentLoaded += new TxDocumentCollection_DocumentLoadedEventHandler(this.OnLoadDocument);
      this.m_selectedFilter = eFilterType.CollisionsOnly;
      this.InitDocumentContext();
    }
    catch (Exception ex)
    {
    }
  }

  private void InitDocumentContext()
  {
    try
    {
      TxDocument activeDocument = TxApplication.ActiveDocument;
      if (activeDocument != null)
        activeDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
      this.m_checkCollisions = TxApplication.ActiveDocument.CollisionRoot.CheckCollisions;
      this.SetQueryContext();
    }
    catch (Exception ex)
    {
    }
  }

  private void UninitDocumentContext()
  {
    try
    {
      TxCollisionRoot collisionRoot = TxApplication.ActiveDocument.CollisionRoot;
      if (this.m_queryGlobalContext != null)
        this.m_queryGlobalContext = (TxCollisionQueryContext) null;
      if (this.m_queryLocalContext != null)
      {
        collisionRoot.DeleteQueryContext(this.m_queryLocalContext);
        this.m_queryLocalContext = (TxCollisionQueryContext) null;
      }
      TxDocument activeDocument = TxApplication.ActiveDocument;
      if (activeDocument == null)
        return;
      activeDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
    }
    catch (Exception ex)
    {
    }
  }

  private void OnLoadDocument(object sender, TxDocumentCollection_DocumentLoadedEventArgs args)
  {
    this.InitDocumentContext();
  }

  private void OnUnloadDocument(object sender, TxDocument_UnloadingEventArgs args)
  {
    this.UninitDocumentContext();
  }

  public eFilterType SelectedFilter
  {
    get => this.m_selectedFilter;
    set => this.m_selectedFilter = value;
  }

  public TxCollisionQueryContext QueryContext
  {
    get => !this.IsLocalContextUsed ? this.m_queryGlobalContext : this.m_queryLocalContext;
  }

  private bool IsLocalContextUsed => eFilterType.DistanceAnalysis == this.m_selectedFilter;
}
